// YouTube.js for PSPTube
// Made By JK108

function YouTube_CheckURL( url, option )
{
	if(url.match("http://www.youtube.com/watch") == "http://www.youtube.com/watch"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a YouTube URL
*/
function YouTube_GetURL( url, option )
{
        var contents = GetContents(url);
	if(contents == null){return null; /* failed to get page */}
	var token = contents.match(/\"t\"\: \"(.*?)\"/);
       	var token = token[1];
	var id = url.match(/watch\?v\=(.*)/);
	var id = id[1];
	if(id == null){return null; /* ID not valid */}
       	var video_url = "http://www.youtube.com/get_video?video_id=" + id + "&t=" + token;
	return video_url;
}

function YouTube_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 20;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	var num = 1 + ((result.start - 1) / perpage);
	var parameter = "t";
	if(keyword.substring(0, 1) == "!"){
		var ind = keyword.indexOf(" ");
		if(ind != -1){
			var newkeyword = keyword.substring(ind + 1, keyword.length);
			if(newkeyword.toLowerCase().substring(0, 5) == "today"){parameter = "t";}
			if(newkeyword.toLowerCase().substring(0, 8) == "thisweek"){parameter = "w";}
			if(newkeyword.toLowerCase().substring(0, 9) == "thismonth"){parameter = "m";}
			if(newkeyword.toLowerCase().substring(0, 7) == "alltime"){parameter = "a";}
		}
	}
	if(keyword.toLowerCase().substring(0, 9) == "!featured"){
		var contents = GetContents( "http://www.youtube.com/browse?s=rf&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 8) == "!popular"){
		if(parameter == "a"){parameter = "t";}
		var contents = GetContents( "http://www.youtube.com/browse?s=pop&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 7) == "!viewed"){
		var contents = GetContents( "http://www.youtube.com/browse?s=mp&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 7) == "!rising"){
		var contents = GetContents( "http://www.youtube.com/browse?s=bzb&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 10) == "!discussed"){
		var contents = GetContents( "http://www.youtube.com/browse?s=md&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 3) == "!hd"){
		var contents = GetContents( "http://www.youtube.com/browse?s=mphd&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 7) == "!recent"){
		var contents = GetContents( "http://www.youtube.com/browse?s=mr&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 10) == "!responded"){
		var contents = GetContents( "http://www.youtube.com/browse?s=ms&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 11) == "!favourited"){
		var contents = GetContents( "http://www.youtube.com/browse?s=mf&t=" + parameter + "&cr=GB&p=" + num );
	} else if(keyword.toLowerCase().substring(0, 6) == "!rated"){
		var contents = GetContents( "http://www.youtube.com/browse?s=tr&t=" + parameter + "&cr=GB&p=" + num );
	} else{
		var contents = GetContents( "http://www.youtube.com/results?search_query=" + keyword.replace(/ /g, "+") + "&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match(" <span >&ldquo;") == " <span >&ldquo;"){ result.total = 0; return result; }
	if(contents.match("</strong> of <strong>millions</strong>") == "</strong> of <strong>millions</strong>"){total = 1000000;}
	else{
		var pos = contents.indexOf(' <strong>');
		contents = contents.substring(pos + 9, contents.length);
		pos = contents.indexOf("</strong>");
		var total = contents.substring(0, pos).replace(/,/g, "");
		if(parseInt(total) == -1){total = 1000000;}
	}
	if(keyword.toLowerCase().substring(0, 9) == "!featured" || keyword.toLowerCase().substring(0, 8) == "!popular" ||
	   keyword.toLowerCase().substring(0, 7) == "!viewed" || keyword.toLowerCase().substring(0, 7) == "!rising" ||
	   keyword.toLowerCase().substring(0, 10) == "!discussed" || keyword.toLowerCase().substring(0, 3) == "!hd" ||
	   keyword.toLowerCase().substring(0, 7) == "!recent" || keyword.toLowerCase().substring(0, 10) == "!responded" ||
	   keyword.toLowerCase().substring(0, 11) == "!favourited" || keyword.toLowerCase().substring(0, 6) == "!rated"
	   ){total = 100;}
	result.total = total;


	while(1){
	var ind3 = contents.indexOf("<div class=\"video-alt-query\">Results for:");
	pos = contents.indexOf("<div class=\"v120WideEntry\"><div class=\"v120WrapperOuter\"><div class=\"v120WrapperInner\"><a id=\"video-url-");
	if(ind3 < pos && ind3 != -1){break;}

	if(pos != -1){
		//Get URL
		contents = contents.substring(pos + 104, contents.length);
		pos = contents.indexOf("\"");
		var url = "http://www.youtube.com/watch?v=" + contents.substring(0, pos);
		//Title
		pos = contents.indexOf(" title=\"");
		contents = contents.substring(pos + 8, contents.length);
		pos = contents.indexOf("\"");
		var title = ImproveText(contents.substring(0, pos));
		//Get Thumbnail
		pos = contents.indexOf(" src=\"");
		contents = contents.substring(pos + 6, contents.length);
		pos = contents.indexOf("\"");
		var thumb_url = contents.substring(0, pos);
		if(thumb_url.substring(thumb_url.length - 4, thumb_url.length) == ".gif"){
			pos = contents.indexOf(" thumb=\"");
			contents = contents.substring(pos + 8, contents.length);
			pos = contents.indexOf("\"");
			var thumb_url = contents.substring(0, pos);
		}
		//Length
		pos = contents.indexOf("<span id=\"video-run-time-");
		contents = contents.substring(pos + 25, contents.length);
		pos = contents.indexOf("\">");
		contents = contents.substring(pos + 2, contents.length);
		pos = contents.indexOf("</span>");
		var length = contents.substring(0, pos);
		var length = TimestringToSeconds(length);
		//Description
		pos = contents.indexOf("class=\"video-description\">");
		contents = contents.substring(pos + 32, contents.length);
		pos = contents.indexOf("\n");
		var description = ImproveText(contents.substring(0, pos));
		if(description == null || description == ""){description = title;}
		description = splitTo2Lines(description, 57);
		//Views
		pos = contents.indexOf("class=\"video-view-count\">");
		contents = contents.substring(pos + 25, contents.length);
		pos = contents.indexOf(" views");
		var views = parseInt(contents.substring(0, pos).replace(/,/g, ""));

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.ViewCount = views;
		info.LengthSeconds = length;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var YouTube = new Object();
YouTube.Name          = "YouTube";		/* Service name */
YouTube.Description   = "YouTube";		/* Service description */
YouTube.SearchDesc    = "YouTube";		/* Service search screen description */
YouTube.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
YouTube.CheckURL      = YouTube_CheckURL;	/* CheckURL function */
YouTube.GetURL        = YouTube_GetURL;		/* GetURL function */
YouTube.Search        = YouTube_Search;		/* Search function */

// add YouTube to site list
SiteList.push( YouTube );
